//Create at 2007.6.20 by benking
//Copyright @ fund123.cn
/*******************************************

latest update: 2007.7.3

history

    [1].base methods complete at 2007.6.20
    [2].add webservices request method at 2007.7.3

*******************************************/

function __ItemCache__(){}

__ItemCache__.m_Cache = {};

__ItemCache__.Add = function( item )
{
	this.m_Cache[item.Name] = item;
};

__ItemCache__.Get = function( name )
{
	return this.m_Cache[name];
};

__ItemCache__.ClearCache = function( menu )
{
    for( var i=0; i<menu.Items.length; ++i )
    {
        var item = menu.Items[i];
        item.Parent = null;
        item.m_Element.Object = null;
        item.m_Element.m_Submi = null;
        item.m_Element = null;
        item.m_ItemPanel = null;
        item.m_ItemLink = null;
        item.Attributes = null;
        
        if( item.SubMenu )
        {
            __ItemCache__.ClearCache(item.SubMenu);
        }
        
        item.SubMenu = null;
        item = null;
    }
    
    __ItemCache__.m_Cache = null;
    menu.Items = null;
    menu.Container = null;
};

function Menu( container )
{
	this.Items = [];
	this.MaxTextWidth = 0;
	this.Container = container;
};

Menu.prototype.Add = function( item )
{	
	__ItemCache__.Add(item);
	var length = CommonLib.TestStrPixel(item.Text);
	if( item.SubMenu )
	{
	    length += 5;
	}
	this.MaxTextWidth = this.MaxTextWidth < length ? length : this.MaxTextWidth;
	item.Parent = this;
    this.Items.push(item);
};

Menu.prototype.GetMenuWidth = function()
{
    return this.MaxTextWidth + 15;
};

Menu.prototype.Render = function( pNode )
{
    var doc = document;
    if( pNode )
    {
        var ul = doc.createElement("UL");
        pNode.appendChild(ul);
        ul.style.width = this.GetMenuWidth();
        for( var i=0; i<this.Items.length; ++i )
        {
            this.Items[i].Render(ul, true);
        }
        
        return ul;
    }
    else
    {
        var tb = doc.createElement("TABLE");
        tb.id = "navmenu";
        this.Container.appendChild(tb);
        
        var tbody = doc.createElement("TBODY");
        tb.appendChild(tbody);
        
        var tr = doc.createElement("TR");
        tbody.appendChild(tr);
        
        for( var i=0; i<this.Items.length; ++i )
        {
            var item = this.Items[i]; 
            var td = doc.createElement("TD");
            tr.appendChild(td);
            item.m_Element = td;
            td.Object = item;   
            td.noWrap  = "true";     
            if( item.SubMenu )
            {
                var a = doc.createElement('A'); 
                td.appendChild(a);
                with( a.style )
                {
                     padding = "0px";
                     margin = "0px";
                }       
                a.href = '#';            
                a.innerHTML = item.Text;   
                
                a.onmouseover = function()
                {
                   this.style.color  = "white"; 
                }  
               
                a.onmouseout = function()
                {
                    this.style.color = "black";
                }  
                                
                var submi = item.SubMenu.Render(td);
                td.m_Submi = submi;
            } 
            else
            {
                var a = doc.createElement('A');
                td.appendChild(a);
                a.innerHTML = item.Text;
                with( a.style )
                {
                     padding = "0px";
                     margin = "0px";
                }  
                a.href = '#'; 
                if( item.Href.Url )
                {
                    a.href = item.Href.Url;
                }   
                if( item.Href.Target )
                {
                    a.target = item.Href.Target;
                }
                                      
                if( item.ClientEvent )
                { 
                    a.onclick = function()
                    {
                        this.parentNode.Object.ClientEvent(this.parentNode);
                    }   
                }
               
                a.onmouseover = function()
                {
                   this.style.color  = "white"; 
                }  
               
                a.onmouseout = function()
                {
                    this.style.color = "black";
                }  
            }
            
            td.onmouseover = function()
            {   
               this.style.backgroundColor = "#6D6D6D";
               this.style.color  = "white"; 
               if( this.m_Submi )
               {
                    this.m_Submi.style.display = "block";
                    
                    if( !CommonLib.IsIE )
                    {
                        this.m_Submi.style.left = this.offsetLeft + "px";
                        this.m_Submi.style.top = this.offsetTop + this.offsetHeight + "px";
                    }
                    else
                    {
                        this.m_Submi.style.top = this.offsetHeight + "px"
                    }
                }    
            }
          
            td.onmouseout = function()
            {
                this.style.backgroundColor = "";
                this.style.color = "black";
                if( this.m_Submi )
                {
                    this.m_Submi.style.display = "none";
                }
            }      
        } 
        
        return tb;
    }
};

Menu.prototype.Show = function()
{
    this.Render();
};

function MenuItem( text, name, eventHandler )
{
    if( !text )
    {
	    this.Text = "item";
	}
	else
	{
	    this.Text = text;
	}
	this.ClientEvent = eventHandler;
	this.Href = {};
	this.Href.Url = "#";
	this.Href.Target = null;
	this.ToolTip = "";
	this.SubMenu = null;
	this.Name = name;
	this.HasTextBox = false;
	this.TextBoxEvent = null;
	this.Parent = null;
	this.Attributes = {};
	this.zIndex = 2000;
};

MenuItem.prototype.SetAttribute = function( key, value )
{
	this.Attributes[key] = value;
};

MenuItem.prototype.GetAttribute = function(key)
{
	return this.Attributes[key];
};

MenuItem.prototype.Render = function( pNode, isSubMenu )
{
    var doc = document;
    var li = doc.createElement("LI");
    pNode.appendChild(li);
    
    li.style.zIndex = this.zIndex--;
    li.Object = this;
    this.m_Element = li;
    this.SubMenuLeft = this.Parent.GetMenuWidth();

    li.onmouseover=function() 
    {
	    this.style.backgroundColor = "#6D6D6D";
	    if( this.Object.m_ItemPanel.tagName != "INPUT" )
	    {
	        this.Object.m_ItemPanel.style.color = "#Fff";	    	
	    }
    	if( this.m_Submi )
    	{
    		with( this.m_Submi.style )
    		{
    			display = "block";
    		}

            this.m_Submi.style.left = this.Object.Parent.GetMenuWidth() + "px";
    	}
	};
	
    li.onmouseout=function() 
    {
        if( isSubMenu )
        {
    	    this.style.backgroundColor = "#fff";
        } 
       else
       {
            this.style.backgroundColor = "";
        }  
    	this.Object.m_ItemPanel.style.color = "black";  	
    	if( this.m_Submi )
    	{
    		with( this.m_Submi.style )
    		{
    			display = "none";
    		}
    	}
    };
	
	if( this.HasTextBox )
	{
	    var box = doc.createElement("INPUT");
	    li.appendChild(box);
	    
	    box.type = "text";
	    box.readOnly = true;
        box.style.width = this.Parent.GetMenuWidth();
	    this.m_ItemPanel = box;
	    box.value = this.Text;
        box.onclick = function()
        {
            if( this.parentNode.Object.ClientEvent )
            {
                this.parentNode.Object.ClientEvent(this.parentNode);
            }
        }
	}
	else
	{
        var a = doc.createElement("A");
        li.appendChild(a);
        
        this.m_ItemLink = a;
        
        if(this.Href.Url)
        {
            a.href = this.Href.Url;
        }
        else
        {
            a.href = "#";
        }   

    	a.style.width = this.Parent.GetMenuWidth() + "px";
    	
        a.onclick = function()
        {
            if( this.parentNode.Object.ClientEvent )
            {
                this.parentNode.Object.ClientEvent(this.parentNode);
            }
            if( this.parentNode.m_Submi )
            {
                this.parentNode.m_Submi.style.display = "none";
            }
            this.parentNode.parentNode.style.display = "none";
        }
        
        if( this.ToolTip )
       { 
            a.title = this.ToolTip;
        } 
        a.style.width = this.Parent.GetMenuWidth() - 6;
 	    
        if( this.Href.Target )
        {
    	    a.target = this.Href.Target;
        }
        var span = doc.createElement('SPAN');
        a.appendChild(span);
        
        this.m_ItemPanel = span;
        span.innerHTML = this.Text;
        
        if( this.SubMenu)
        {
            var hint = doc.createElement('SPAN');
            hint.id = "hint";
            a.appendChild(hint);
            if( CommonLib.IsIE )
            {
                hint.innerHTML = '4';
                hint.style.fontFamily = 'Webdings';
                hint.style.margin = "-4px";
            }
            else
            {
                hint.innerHTML = '&#9658;';
            }
        }
        
        li.appendChild(a);
    }
    
    if( this.SubMenu )
    {
    	var submi = this.SubMenu.Render( li );
    	li.m_Submi = submi;
    }
};

